package edu.ccut.saturn.studio.validate.wizard;

import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.internal.ui.dialogs.TypeSelectionDialog2;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;
import edu.ccut.saturn.studio.validate.dialog.CreateClassDialog;

/**
 * 
 * @author Bai Yucheng 2007-11-26
 * 
 */
public class ValidateWizardPage extends SaturnWizardPage {

	private static final String TIATN_VALIDATEWIZARD_CUSTOMIZEVALIDATE = "customizeValidate"; //$NON-NLS-1$
	private static final String TIATN_VALIDATEWIZARD_JSVALIDATE = "jsValidate"; //$NON-NLS-1$
	private static final String TIATN_VALIDATEWIZARD_DESCRIPTION = "description"; //$NON-NLS-1$
	private static final String TIATN_VALIDATEWIZARD_CLASS = "clazz"; //$NON-NLS-1$
	private static final String TIATN_VALIDATEWIZARD_ID = "id"; //$NON-NLS-1$
	public static final String TIATN_VALIDATEWIZARD_NAME = "name"; //$NON-NLS-1$

	private Text validateIdText;
	private Text validateNameText;
	private Text descriptionText;
	private Text ValidateClassText;
	private Text customizeValidateText;
	private Text jsValidateText;

	private String projectFilePath;
	private IProject project;

	public ValidateWizardPage() {
		setTitle(Messages.getString("ValidateWizardPage.ValidateWizard")); //$NON-NLS-1$
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		container.setLayout(gridLayout);
		setControl(container);

		final Label className = new Label(container, SWT.NONE);
		className.setLayoutData(new GridData());
		className
				.setText(Messages.getString("CreateValidateFileWizardPage.validateclass")); //$NON-NLS-1$

		ValidateClassText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		ValidateClassText.setTextLimit(100);
		final GridData gd_validateClassText = new GridData(SWT.FILL,
				SWT.CENTER, true, false);
		gd_validateClassText.widthHint = 349;
		ValidateClassText.setLayoutData(gd_validateClassText);
		ValidateClassText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		final Button button = new Button(container, SWT.NONE);
		button.setLayoutData(new GridData(50, SWT.DEFAULT));

		button.addSelectionListener(new SelectionAdapter() {
			@SuppressWarnings( { "deprecation", "restriction" })//$NON-NLS-1$ //$NON-NLS-2$
			public void widgetSelected(final SelectionEvent e) {
				IJavaSearchScope searchScope = SearchEngine
						.createWorkspaceScope();
				TypeSelectionDialog2 dialog = new TypeSelectionDialog2(
						getShell(), false,
						new ProgressMonitorDialog(getShell()), searchScope,
						IJavaSearchConstants.TYPE);
				dialog.setMessage(Messages
						.getString("CreateValidateFileWizardPage.pleasewritevalidatename")); //$NON-NLS-1$
				dialog.setBlockOnOpen(true);
				dialog.setTitle(Messages
						.getString("CreateValidateFileWizardPage.choosevalidateclass")); //$NON-NLS-1$
				if (Dialog.OK == dialog.open()) {
					IType obj = (IType) dialog.getFirstResult();
					ValidateClassText.setText(obj.getFullyQualifiedName()
							.trim());
				}
			}
		});
		button.setText(Messages.getString("CreateValidateFileWizardPage.browsing")); //$NON-NLS-1$

		final Button createClassButton = new Button(container, SWT.NONE);
		createClassButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				projectFilePath = getPackageName();
				CreateClassDialog classDialog = new CreateClassDialog(getShell(),
						getProject(), projectFilePath);
				// classDialog.setSelection(selection);
				if (classDialog.open() == IDialogConstants.OK_ID) {
					ValidateClassText.setText(classDialog.getPkgName()
							+ "." + classDialog.getClassName()); //$NON-NLS-1$
				}
			}
		});
		final GridData gd_createClassButton = new GridData(50, SWT.DEFAULT);
		createClassButton.setLayoutData(gd_createClassButton);
		createClassButton.setText(Messages
				.getString("CreateValidateFileWizardPage.create")); //$NON-NLS-1$

		final Label keyLabel = new Label(container, SWT.NONE);
		final GridData gd_keyLabel = new GridData(60, SWT.DEFAULT);
		keyLabel.setLayoutData(gd_keyLabel);
		keyLabel.setText(Messages.getString("CreateValidateFileWizardPage.validateid")); //$NON-NLS-1$

		GridData gd = new GridData();

		validateIdText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		validateIdText.setTextLimit(100);
		final GridData gd_validateIdText = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		validateIdText.setLayoutData(gd_validateIdText);

		validateIdText.setText(getComponentKey("")); //$NON-NLS-1$

		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label nameLabel = new Label(container, SWT.NONE);
		nameLabel.setLayoutData(new GridData());
		nameLabel
				.setText(Messages.getString("CreateValidateFileWizardPage.name")); //$NON-NLS-1$

		validateNameText = new Text(container, SWT.BORDER);
		validateNameText.setTextLimit(50);
		final GridData gd_validateNameText = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		validateNameText.setLayoutData(gd_validateNameText);
		validateNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validateIdText.setText(getComponentKey(validateNameText.getText()
						+ ".validate")); //$NON-NLS-1$
				validate();
			}
		});

		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label descriptionLabel = new Label(container, SWT.NONE);
		descriptionLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
				true));
		descriptionLabel.setText(Messages
				.getString("CreateValidateFileWizardPage.description")); //$NON-NLS-1$

		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER
				| SWT.WRAP | SWT.H_SCROLL);
		descriptionText.setTextLimit(200);
		descriptionText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		gd = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd.heightHint = 40;
		descriptionText.setLayoutData(gd);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label label = new Label(container, SWT.NONE);
		label.setText(Messages.getString("CreateValidateFileWizardPage.jsvalidate")); //$NON-NLS-1$

		final GridData gd_jsValidateText = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		jsValidateText = new Text(container, SWT.BORDER);
		jsValidateText.setTextLimit(50);
		jsValidateText.setLayoutData(gd_jsValidateText);
		jsValidateText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});

		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
		label_1.setText(Messages.getString("CreateValidateFileWizardPage.userdefinejsvalidate")); //$NON-NLS-1$

		customizeValidateText = new Text(container, SWT.V_SCROLL | SWT.BORDER
				| SWT.WRAP | SWT.H_SCROLL);
		customizeValidateText.setTextLimit(1000);
		customizeValidateText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_customizeValidateText = new GridData(SWT.FILL,
				SWT.FILL, true, true);
		gd_customizeValidateText.widthHint = 258;
		gd_customizeValidateText.heightHint = 153;
		customizeValidateText.setLayoutData(gd_customizeValidateText);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		validate();

	}

	public boolean validate() {
		setPageComplete(false);

		if (SaturnValidation.isEmpty(this.ValidateClassText.getText().trim())) {
			setErrorMessage(Messages.getString("ValidateWizardPage.pleasechoosevalidateclass"));  //$NON-NLS-1$
		} else if (SaturnValidation.isEmpty(this.validateNameText.getText()
				.trim())) {
			setErrorMessage(Messages.getString("ValidateWizardPage.pleasewritevalidateclassname"));  //$NON-NLS-1$
		} else if (!SaturnValidation.isValidProgramStr(this.validateNameText
				.getText().trim())) {
			setErrorMessage(Messages
					.getString("CreateValidateFileWizardPage.nameelement")); //$NON-NLS-1$
		
//		} else if (descriptionText.getText().trim().length() > 200) {
//			setErrorMessage(Messages.getString("ValidateWizardPage.maxlengthlimitof200")); //$NON-NLS-1$
//		} else if (jsValidateText.getText().trim().length() >50) {
//			setErrorMessage(Messages.getString("ValidateWizardPage.maxlengthlimitof50")); //$NON-NLS-1$
//		}else if (customizeValidateText.getText().trim().length() ==1000) {
//			setErrorMessage(Messages.getString("ValidateWizardPage.maxlengthlimitof1000")); //$NON-NLS-1$
		} else {
			setErrorMessage(null);
			setPageComplete(true);
			return true;
		}

		return false;
	}

	@Override
	public void initalValues(Map<String, Object> values) {
		values.put(TIATN_VALIDATEWIZARD_NAME, this.validateNameText.getText());
		values.put(TIATN_VALIDATEWIZARD_ID, this.validateIdText.getText());
		values.put(TIATN_VALIDATEWIZARD_CLASS, this.ValidateClassText
						.getText());
		values.put(TIATN_VALIDATEWIZARD_DESCRIPTION, this.descriptionText
				.getText());
		values.put(TIATN_VALIDATEWIZARD_JSVALIDATE, this.jsValidateText
				.getText());
		values.put(TIATN_VALIDATEWIZARD_CUSTOMIZEVALIDATE,
				this.customizeValidateText.getText());

	}

	private IProject getProject() {
		if (project == null) {
			this.project = SaturnProjectUtil.getProject(super.getSelection());
		}
		
		return this.project;
	}
}
